package com.mayikt;

import java.util.ArrayList;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @ClassName MayiktLock
 * @Author 蚂蚁课堂余胜军 QQ644064779 www.mayikt.com
 * @Version V1.0
 **/
public class MayiktLock implements Runnable {
    private static int count = 0;
    private static Lock lock = new ReentrantLock(false);

    @Override
    public void run() {

        while (count < 200) {
//            try {
//                Thread.sleep(10);
//            } catch (Exception e) {
//
//            }
            lock.lock();
            createCount();
            lock.unlock();
        }
    }

    public void createCount() {
        System.out.println(Thread.currentThread().getName() + ",count:" + count);
        count++;
    }

    public static void main(String[] args) {
        long startTime = System.currentTimeMillis();
        ArrayList<Thread> threads = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            Thread thread = new Thread(new MayiktLock());
            thread.start();
            threads.add(thread);
        }
        threads.forEach((t) -> {
            try {
                t.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        long endTime = System.currentTimeMillis();
        System.out.println(endTime - startTime);
    }
}
